/**
 * @context 创建奖品 选择奖品类型
 * @param mark
 */
function createPrize(mark,active_id='')
{
    let btn=["积分","金币","卡券","恢复券"];
    // if(mark==1){
    //     btn.push("其他物品");
    // }
    layer.open({
        type:1,
        skin:"prizeconfirm",
        title: "请选择奖品类型",
        content:false,
        area:['450px',"130px"],
        maxWidth:'300px',
        btn: btn,
        btn1: function (index, layero) {
            // 选择积分
            confirmPrize(1,mark,active_id);
            layer.close(index);
            return false

        },
        btn2: function (index, layero) {
            // 选择金币
            confirmPrize(2,mark,active_id);
            layer.close(index);
            return false
        } ,
        btn3: function (index, layero) {
            // 选择卡券
            confirmPrize(3,mark,active_id);
            layer.close(index);
            return false
        },
        btn4: function (index, layero) {
            // 选择恢复券
            confirmPrize(4,mark,active_id);
            layer.close(index);
            return false
        },
        btn5: function (index, layero) {
            // 选择其他物品
            confirmPrize(5,mark,active_id);
            layer.close(index);
            return false
        },
        cancel: function () {
            //右上角关闭回调
            //return false 开启该代码可禁止点击该按钮关闭
            layer.close();
        }
    });
}

/**
 * @context 创建奖品表单
 * @param type
 * @param mark
 */
function confirmPrize(type,mark,active_id)
{
    var confirmprize=layer.open({
        type: 2 ,
        title: "添加奖品",
        area:['100%','100%'],
        shade: 0.5,
        maxmin: true,
        content: '/admin/Prize/create?type='+type+"&mark="+mark+"&active_id="+active_id
    });
}

/**
 * @context 奖品列表
 * @param mark
 * @returns {boolean}
 */
function getsPrizeList(mark)
{
    $.ajax({
        url: "/admin/Prize/getsPrizeList",
        type: 'post',
        async: true,
        data: {
            mark
        },
        dataType: 'json',
        success: function (res) {
            if(res.status){
                $("#prize_table").html(res.data);
            }
        }
    });
    return false;
}

/**
 * @context 删除奖品
 * @param obj
 * @param id
 * @param mark
 * @param storage
 */
function deletePrize(obj,id,mark,storage,db_id){
    $.ajax({
        url: "/admin/Prize/deletePrize",
        type: 'post',
        async: true,
        data: {
            mark,
            id,
            storage,
            db_id
        },
        dataType: 'json',
        success: function (res) {
            if(res.status){
                if(obj!=''&&obj!=undefined){
                    $(obj).parent().parent().remove();
                }
            }
        }
    });
}

/**
 * @context 修改奖品信息
 * @param obj
 * @param id    session的key 或者 db的主键
 * @param mark  奖品的标识 1 大转轮奖品
 * @param storage   存储的位置
 * @param field     修改的字段
 * @param type      字段类型
 */
function changePrizeInfo(obj,id,db_id,mark,storage,field,type)
{
    let old_param=$(obj).data("param");
    let str="<div class=\"layui-form-item\" style='width:80%;margin-left:10%;'>\n";
    if(type==1){
        str+= "<input type=\"number\" style='margin-top:10%;' value='"+old_param+"' id='new_param' lay-verify=\"required\" step=\"1\" min='1' value=\"\" class=\"layui-input\">\n" ;
    }else{
        str+= "<input type=\"number\" style='margin-top:10%;' id='new_param' value='"+old_param+"' lay-verify=\"required\" min='0' max=\"1\" step=\"0.01\" value=\"\" class=\"layui-input\">\n" ;
    }
    str += "</div>\n" +
        "<div class=\"layui-form-item\" style='text-align: center'>\n" +

        " <button class=\"layui-btn layui-btn-normal\" id='confirmChange'>确认保存</button>\n" +
        "    </div>";
    let confirmOpen=layer.open({
        type: 1 ,
        title: "修改奖品",
        area:['500px', '300px'],
        shade: 0.5,
        maxmin: true,
        content: str
    })
    $("#confirmChange").click(function(){
        let new_param=$("#new_param").val();
        if(new_param==''||new_param==undefined){
            layer.msg("必填项不能为空");
        }else{
            $.ajax({
                url: "/admin/Prize/confirmChangePrizeInfo",
                type: 'post',
                async: true,
                data: {
                    id,
                    mark,
                    storage,
                    field,
                    new_param,
                    db_id
                },
                dataType: 'json',
                success: function (res) {
                    if(res.status){
                        $(obj).data("param",new_param);
                        $(obj).html(new_param);
                        layer.close(confirmOpen);
                    }else{
                        return false;
                    }
                }
            });
        }
    });

}